//
//  HMStatusPictureView.m
//  HMWeiBoOC
//
//  Created by GD on 16/9/26.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "HMStatusPictureView.h"
#import "HMGlobalConst.h"
#import "UILabel+HMLabel.h"
#import "HMStatusPictureInfo.h"
#import <UIImageView+WebCache.h>
#import <SDWebImageManager.h>




static NSString *HMStatusPictureViewCellIdentifier = @"HMStatusPictureViewCellIdentifier";

CGFloat itemMargin = 5;
//CGFloat itemWidth = (ScreenWidth - 2 * HMHomeTableViewCellMargin - 2 * itemMargin) / 3;编译时,运行时 (全局变量)
#define itemWidth (ScreenWidth - 2 * HMHomeTableViewCellMargin - 2 * itemMargin) / 3

@interface HMStatusPictureView ()<UICollectionViewDataSource>

@property (nonatomic,strong) UILabel *messageLabel;

@end

@implementation HMStatusPictureView

-(void)setPicUrls:(NSArray *)picUrls{
    _picUrls = picUrls;
    
    self.messageLabel.text = [NSString stringWithFormat:@"%zd",picUrls.count];
    
    CGSize size = [self calcSizeWithCount:picUrls.count];
    
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(size);
    }];
    
    [self reloadData];
}


//-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
//    
//    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//    flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth + 10);
//    flowLayout.minimumLineSpacing = itemMargin;
//    flowLayout.minimumInteritemSpacing = itemMargin;
//    [self setupUI];
//   
//    return [super initWithFrame:frame collectionViewLayout:flowLayout];
//    
//    
//}

-(instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth + 10);
    flowLayout.minimumLineSpacing = itemMargin;
    flowLayout.minimumInteritemSpacing = itemMargin;
    self = [super initWithFrame:frame collectionViewLayout:flowLayout];
    if (self) {
        [self setupUI];
    }
    return self;
}



-(void)setupUI{
    self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    
    [self registerClass:[HMStatusPictureViewCell class] forCellWithReuseIdentifier:HMStatusPictureViewCellIdentifier];
    
    self.dataSource =self;
    
    [self addSubview:self.messageLabel];
    
    [_messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
//    NSLog(@"AA");
}

-(CGSize )calcSizeWithCount:(int)count{
    //  判断图片个数是否等于1,如果图片的个数等于1 表示要从缓存中去图片
    if (count == 1) {
        NSString *imageUrl = self.picUrls.firstObject.thumbnail_pic;
        UIImage *image = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:imageUrl];
   
        if (image != nil) {
            CGSize size = image.size;
            if (size.width < 80) {//宽高比例严重失调,需特殊处理
                CGFloat scaleWidth = 80;
                CGFloat height = scaleWidth /size.width * size.height;
                
                CGFloat scaleHeight = height > 150 ? 150 :height;
                size.width = scaleWidth;
                size.height = scaleHeight;
            }
            UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
            flowLayout.itemSize = size;
            return size;
        }
        
    }
    UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
    flowLayout.itemSize = CGSizeMake(itemWidth, itemWidth);
    
    int cols = count > 3 ? 3 : count;
    int rows = (count - 1) / 3 + 1;
    
    CGFloat currentWidth = itemWidth *cols + (cols - 1)* itemMargin;
    CGFloat currentHeight = (itemWidth + 10) * rows + (rows - 1) * itemMargin;
    
    return CGSizeMake(currentWidth, currentHeight);
    
}

#pragma mark -UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.picUrls.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    HMStatusPictureViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HMStatusPictureViewCellIdentifier forIndexPath:indexPath];
    cell.statusPictureInfo = self.picUrls[indexPath.row];
    
    return cell;

}


 //  MARK: --懒加载控件

-(UILabel *)messageLabel{
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc] initWithTextColor:[UIColor orangeColor] fontSize:[UIFont systemFontOfSize:17]];
    }
    return _messageLabel;
}

@end




@implementation HMStatusPictureViewCell

-(void)setStatusPictureInfo:(HMStatusPictureInfo *)statusPictureInfo{
    _statusPictureInfo = statusPictureInfo;
    
    NSString *url = statusPictureInfo.thumbnail_pic;
    [_imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"timeline_image_placeholder"]];
}


-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    [self.contentView addSubview:self.imageView];
    
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView);
    }];
}



 //  MARK: --懒加载控件
-(UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"timeline_image_placeholder"]];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.clipsToBounds = YES;
    }
    return _imageView;
}

@end



